﻿using System;
using System.Collections;
using System.Linq;
using System.Text;
using System.Windows.Media.Media3D;
using Microsoft.Research.Kinect.Nui;


namespace KiMoCSys.kinectDevice
{
    class kDevice
    {
        private Runtime nui = null;
        private Model3DGroup container;
        ArrayList nodes;

        public bool init(Model3DGroup container)
        {
            try
            {
                this.container = container;
                nui = new Runtime();
                nodes = new ArrayList();
                //initialize the kinect's sensor
                nui.Initialize(RuntimeOptions.UseSkeletalTracking | RuntimeOptions.UseDepth | RuntimeOptions.UseDepthAndPlayerIndex |
                                RuntimeOptions.UseColor);
                nui.SkeletonFrameReady += new EventHandler<SkeletonFrameReadyEventArgs>(onSkeletonFrameReady);
                return true;
            }
            catch (Exception ex)
            {
                nui = null;
                return false;
            }           
        }

        /// <summary>
        /// Called when a frame is ready after processing the skeleton data
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        void onSkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs args)
        {
            nodes.Clear();
            SkeletonFrame skeleton = args.SkeletonFrame;
            foreach (SkeletonData data in skeleton.Skeletons)
            {
                if (data.UserIndex != 0)
                    nodes.Add(new kNode(20, data));
            }

            GeometryModel3D model;
            //container.Children.Clear();
            foreach (kNode node in nodes)
            {
                model = new GeometryModel3D(node.getBuiltSphere(), new DiffuseMaterial(System.Windows.Media.Brushes.Blue));
                model.Transform = new Transform3DGroup();
                container.Children.Add(model);
            }
        }

        public void stop()
        {
            if (nui != null)
                nui.Uninitialize();
        }
        

    }
}
